<?php

namespace app\yj\controller;

use app\yj\model\NoticeModel;
use think\Controller;
use think\Request;

class NoticeController extends BaseAdminController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        //
        $method = $request->method();
        if (strtolower($method) == 'get') {
            //获取会议
            $all = $request->get();
            $page = $request->get('page');
            $size = $request->get('size');
            $id = $request->get('id');
            $name = $request->get('name');
            $createTime_min = $request->get('createTime_min');
            $createTime_max = $request->get('createTime_max');
            //获取某个指定的 
            $noticesql = NoticeModel::order('id', 'desc');
            if ($page) $noticesql->limit(($page - 1) * $size, $size);
            $noticesql = $noticesql->order('id', 'desc');
            if ($id) $noticesql->where('id', $id);
            if ($name) $noticesql->where('name', 'like', '%' . $name . '%');
            if ($createTime_min) $noticesql->where('addtime', '>=', $createTime_min);
            if ($createTime_max) $noticesql->where('addtime', "<=", $createTime_max);
            $notices = $noticesql->select()->toArray();
            $count = $noticesql->count();
   
            return $this->SuccessReturn('ok', $notices, $count);
        }

        if (strtolower($method) == 'post') {
            $all = $request->post();

            //判断是否存在
            $name = key_exists('name', $all) ? $all['name'] : '未定义';
            $imgs = key_exists('imgs', $all) ? $all['imgs'] : [];
            $fj = key_exists('fj', $all) ? $all['imgs'] : [];
            $context = key_exists('context', $all) ? $all['context'] : '';
            $see = [];
            $addtime = time();

            $notic = new NoticeModel();
            $notic->name = $name;
            $notic->imgs = $imgs;
            $notic->fj = $fj;
            $notic->context = $context;
            $notic->see = $see;
            $notic->addtime = $addtime;
            $notic->save();

            return $this->SuccessReturn('添加成功', $notic);

        }

        if (strtolower($method) == 'put') {
            $all = $request->post();
//判断是否存在
            $name = key_exists('name', $all) ? $all['name'] : null;
            $imgs = key_exists('imgs', $all) ? $all['imgs'] : null;
            $fj = key_exists('fj', $all) ? $all['fj'] : null;
            $context = key_exists('context', $all) ? $all['context'] : null;
            $puttime = time();

            $notic = NoticeModel::find($all['id']);
            if (!$notic) return $this->ErrorReturn('未找到相关通知');
            if ($name != null) {
                $notic->name = $name;
            }
            if ($imgs != null) {
                $notic->imgs = $imgs;
            }
            if ($fj != null) {
                $notic->fj = $fj;
            }
            if ($context != null) {
                $notic->context = $context;
            }
            $notic->puttime = $puttime;
            $notic->save();
            return $this->SuccessReturn('修改成功', $notic);
        }

        if (strtolower($method) == 'delete') {
            $id = $request->delete('id');
            NoticeModel::destroy($id);
            return $this->SuccessReturn('ok');
        }

    }

    public function userseenotic(Request $request)
    {
        $noticeid = $request->post('noticeid');
        $userid = $request->post('userid');
        //获取通知
        $notice = NoticeModel::find($noticeid);
        if (!$notice) return $this->ErrorReturn(500, '未找到对应的通知');
        if (!in_array($userid, $notice['see'])) {
            $aa = $notice['see'];
            array_push($aa, (int)$userid);
            $notice->see = $aa;
            $notice->save();
        }
        return $this->SuccessReturn('ok', $notice);
    }
}
